package ink.yuanchu.picachoframework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.yuanchu.picachoframework.dao.PermissionsMapper;
import ink.yuanchu.picachoframework.dao.RoleAssociatedPermissionsMapper;
import ink.yuanchu.picachoframework.entity.Permissions;
import ink.yuanchu.picachoframework.entity.RoleAssociatedPermissions;
import ink.yuanchu.picachoframework.service.RoleAssociatedPermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ziyuan
 * @Date: 2021/03/20/14:05
 * @Description:
 */
@Service
public class RoleAssociatedPermissionsServiceImpl extends ServiceImpl<RoleAssociatedPermissionsMapper, RoleAssociatedPermissions> implements RoleAssociatedPermissionsService {

    @Autowired
    private RoleAssociatedPermissionsMapper roleAssociatedPermissionsMapper;

    @Autowired
    private PermissionsMapper permissionsMapper;

    /**
     * 根据角色id查找所有权限
     * @param roleId
     * @return
     */
    @Override
    public List<Permissions> findAllPermissionsByRoleId(String roleId) {
        List<Permissions> permissionsList = new ArrayList<>();
        QueryWrapper<RoleAssociatedPermissions> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<RoleAssociatedPermissions> roleAssociatedPermissions = roleAssociatedPermissionsMapper.selectList(wrapper);
        for (RoleAssociatedPermissions roleAssociatedPermission : roleAssociatedPermissions) {
            String permissionsId = roleAssociatedPermission.getPermissionsId();
            Permissions permissions = permissionsMapper.selectById(permissionsId);
            permissionsList.add(permissions);
        }
        return permissionsList;
    }

    /**
     * 根据角色id和权限id删除某个权限
     * @param roleId
     * @param permissionsId
     * @return
     */
    @Override
    public int deleteByRoleIdAndPermissionsId(String roleId, String permissionsId) {
        QueryWrapper<RoleAssociatedPermissions> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        wrapper.eq("permissions_id",permissionsId);
        int delete = roleAssociatedPermissionsMapper.delete(wrapper);
        return delete;
    }

    /**
     * 给角色添加权限
     * @param roleId
     * @param permissionId
     * @return
     */
    @Override
    public int addRoleAssociatedPermissions(String roleId, String permissionId) {
        QueryWrapper<RoleAssociatedPermissions> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        wrapper.eq("permissions_id",permissionId);
        Integer integer = roleAssociatedPermissionsMapper.selectCount(wrapper);
        //已经存在就不插入
        if (integer != 0){
            return integer;
        }
        RoleAssociatedPermissions roleAssociatedPermissions = new RoleAssociatedPermissions();
        roleAssociatedPermissions.setRoleId(roleId);
        roleAssociatedPermissions.setPermissionsId(permissionId);
        int insert = roleAssociatedPermissionsMapper.insert(roleAssociatedPermissions);
        return insert;
    }
}
